package com.ilovepostcard.view;

import com.ilovepostcard.Actions;
import com.ilovepostcard.MainActivity;
import com.ilovepostcard.R;
import com.ilovepostcard.controller.BaseAsyncTask;
import com.ilovepostcard.controller.ServerAsyncTask;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.StringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class PostcardLoginView extends LinearLayout {

    public PostcardLoginView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate();
    }

    public PostcardLoginView(Context context) {
        super(context);
        inflate();
    }

    private void inflate() {
        View view = View.inflate(getContext(), R.layout.postcard_login, this);
        final EditText emailView = (EditText)view.findViewById(R.id.login_email);
        final EditText pwdView = (EditText)view.findViewById(R.id.login_password);
        view.findViewById(R.id.login_button).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        final String email = emailView.getText().toString().trim();
                        final String pwd = pwdView.getText().toString();
                        if (StringUtils.isEmpty(email)) {
                            Toast.makeText(getContext(), R.string.notify_input_email, Toast.LENGTH_SHORT).show();
                            emailView.requestFocus();
                            return;
                        }
                        if (StringUtils.isEmpty(pwd)) {
                            Toast.makeText(getContext(), R.string.notify_input_pwd, Toast.LENGTH_SHORT).show();
                            pwdView.requestFocus();
                            return;
                        }
                        new ServerAsyncTask(getContext()) {
                            
                            @Override
                            protected void onSuccess(PostcardResult result) {
                                User user = new User();
                                /* the result is uid */
                                user.UserID = result.getResult();
                                user.Username = email;
                                UserController.saveLoginUser(context, user);
                                /* now only used to notify the LoginActivity to finish itself. */
                                context.sendBroadcast(new Intent(Actions.ACTION_LOGIN_SUCCESS));
                                ((Activity)context).finish();
                                Intent intent = new Intent(context, MainActivity.class);
                                context.startActivity(intent);
                            }
                            
                            @Override
                            protected PostcardResult doRequest() {
                                return ServerFactory.getServer().login(email, pwd);
                            }
                        }.execute();
                    }
                });
    }
}
